package com.databinding.jetpacktesting.room;

import android.content.Context;
import android.os.AsyncTask;

import androidx.lifecycle.LiveData;

import java.util.List;

/**
 * Created by harisucici on 2020/3/15
 * Class description:
 */
public class WordContainer {

    private LiveData<List<WordEntity>> allWordsLive;
    private WordDao wordDao;
    private LiveData<List<WordEntity>> allWordsLife;

    public WordContainer(Context context) {
        WordDataBase wordDataBase = WordDataBase.getInstance(context.getApplicationContext());
        wordDao = wordDataBase.getWordDao();
        allWordsLive = wordDao.getAllWordsLife();
    }

    public LiveData<List<WordEntity>> getAllWordsLive() {
        return allWordsLive;
    }

    public void setAllWordsLive(LiveData<List<WordEntity>> allWordsLive) {
        this.allWordsLive = allWordsLive;
    }


    public void insertWords(WordEntity... wordEntities) {
        new InsertAsyncTask(wordDao).execute(wordEntities);
    }

    public void updateWords(WordEntity... wordEntities) {
        new UpdateAsyncTask(wordDao).execute(wordEntities);
    }

    public void deleteWords(WordEntity... wordEntities) {
        new DeleteAsyncTask(wordDao).execute(wordEntities);
    }

    public void deleteAll() {
        new DeleteAllAsyncTask(wordDao).execute();
    }

    //内部类必须 static  否则内存泄露 两个Void 进度,结果
    static class InsertAsyncTask extends AsyncTask<WordEntity, Void, Void> {
        private WordDao wordDao;

        public InsertAsyncTask(WordDao wordDao) {
            this.wordDao = wordDao;
        }

        @Override
        protected Void doInBackground(WordEntity... wordEntities) {
            wordDao.insertWords(wordEntities);
            publishProgress();
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {//任务完成呼叫带给主线程结果
            super.onPostExecute(aVoid);
        }

        @Override
        protected void onProgressUpdate(Void... values) {//呼叫进度
            super.onProgressUpdate(values);
        }

        @Override
        protected void onPreExecute() {//后台执行前
            super.onPreExecute();
        }
    }

    static class UpdateAsyncTask extends AsyncTask<WordEntity, Void, Void> {
        private WordDao wordDao;

        public UpdateAsyncTask(WordDao wordDao) {
            this.wordDao = wordDao;
        }

        @Override
        protected Void doInBackground(WordEntity... wordEntities) {
            wordDao.update(wordEntities);
            publishProgress();
            return null;
        }


    }

    static class DeleteAsyncTask extends AsyncTask<WordEntity, Void, Void> {
        private WordDao wordDao;

        public DeleteAsyncTask(WordDao wordDao) {
            this.wordDao = wordDao;
        }

        @Override
        protected Void doInBackground(WordEntity... wordEntities) {
            wordDao.deleteWords(wordEntities);
            publishProgress();
            return null;
        }


    }

    static class DeleteAllAsyncTask extends AsyncTask<Void, Void, Void> {
        private WordDao wordDao;

        public DeleteAllAsyncTask(WordDao wordDao) {
            this.wordDao = wordDao;
        }

        @Override
        protected Void doInBackground(Void... voids) {
            wordDao.deletAll();
            publishProgress();
            return null;
        }
    }
}
